package com.itheima.controller;

import com.itheima.common.PageBean;
import com.itheima.common.R;
import com.itheima.dto.EmpExprDto;
import com.itheima.dto.QueryEmpDto;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import com.itheima.vo.EmpVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emps")
@Slf4j
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping
    public R pageQuery(QueryEmpDto dto) {
        log.info("分页查询所有员工:{}", dto);
        PageBean list = empService.pageQuery(dto);
        return R.ok(list);
    }
    @GetMapping("/list")
    public R  getAll(){
     List <Emp> emp =empService.getAll();
      return R.ok(emp);
    }

    @PostMapping
    public R add(@RequestBody EmpExprDto dto) {
        log.info("添加员工：{}", dto);
        empService.add(dto);
        return R.ok();
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        log.info("根据id查询：{}", id);
        EmpVo empVo = empService.getById(id);
        return R.ok(empVo);
    }

    @PutMapping
    public R update(@RequestBody EmpExprDto dto) {
        log.info("修改员工：{}", dto);
        empService.updataById(dto);
        return R.ok();
    }

    @DeleteMapping
    public  R deleteByIds( @RequestParam  Integer[] ids ){
        log.info("根据id删除员工：{}",ids);
        empService.deleteByIds(ids);
        return  R.ok();

    }
}
